package com.yzc.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.Map;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ModUtils {
	
	private static void saveDocument(Document document,String setting){
		TransformerFactory facotry = TransformerFactory.newInstance();
		Transformer ts;
		try {
			ts = facotry.newTransformer();
			ts.transform(new DOMSource(document), new StreamResult(setting));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void modSetting(Map<String,String> argsMap,Document document,String setting){
		NodeList nodeList = document.getElementsByTagName("installed");
		for (int i = 0; i < nodeList.getLength(); i++) {  //对 installed 节点循环
			Node node = nodeList.item(i);
			NamedNodeMap namedNodeMap = node.getAttributes();  //获取单个installed节点的属性集
			
			for (int j = 0; j < namedNodeMap.getLength(); j++) {  //对每个属性对进行遍历
				Node attrNode = namedNodeMap.getNamedItem("facet");
				if (attrNode.getNodeValue().equals("java")) {
					Node targetNode = namedNodeMap.getNamedItem("version");
					targetNode.setNodeValue(argsMap.get("jdk"));
//					System.out.println(targetNode.getNodeName()+"---"+targetNode.getNodeValue());
					break;
				}
				if (attrNode.getNodeValue().equals("jst.web")) {
					Node targetNode = namedNodeMap.getNamedItem("version");
					targetNode.setNodeValue(argsMap.get("servlet"));
//					System.out.println(targetNode.getNodeName()+"---"+targetNode.getNodeValue());
					break;
				}
				
			}
			
		}
		
//		NodeList faceted_project_NodeList = document.getElementsByTagName("faceted-project");
//		Node faceted_project = faceted_project_NodeList.item(0);
//		Element runtime = document.createElement("runtime");
//		runtime.setAttribute("name", "Apache Tomcat v" + argsMap.get("runtime"));
//		faceted_project.appendChild(runtime);
		
		saveDocument(document, setting);
	}
	
	public static void modPom(Map<String,String> argsMap,Document document,String pom){
		NodeList nodeList = document.getElementsByTagName("build");
		Node buildNode = nodeList.item(0);
		Element plugins = document.createElement("plugins");
		Element plugin = document.createElement("plugin");
		plugins.appendChild(plugin);
		Element artifactId = document.createElement("artifactId");
		artifactId.setTextContent("maven-compiler-plugin");
		plugin.appendChild(artifactId);
		
		Element version = document.createElement("version");
		version.setTextContent("3.0");
		plugin.appendChild(version);
		
		Element configuration = document.createElement("configuration");
		plugin.appendChild(configuration);
		
		Element source = document.createElement("source");
		source.setTextContent(argsMap.get("jdk"));
		configuration.appendChild(source);
		
		Element target = document.createElement("target");
		target.setTextContent(argsMap.get("jdk"));
		configuration.appendChild(target);
		
		buildNode.appendChild(plugins);
		
		saveDocument(document, pom);
	}
	
	public static void modWeb(String webxml,BufferedReader bufferedReader) throws Exception{
		File oldWebXml = new File(webxml);
		oldWebXml.delete();
		
		File newWebXml = new File(webxml);
		if (!newWebXml.exists()) {
			newWebXml.createNewFile();
		}
		
		BufferedWriter bufferedWriter = 
				new BufferedWriter(new OutputStreamWriter(new FileOutputStream(newWebXml)));
		
		String line = "";
		
		while( !((line=bufferedReader.readLine())==null) ){
			bufferedWriter.write(line);
			bufferedWriter.newLine();
		}
		bufferedWriter.flush();
		bufferedWriter.close();
		bufferedReader.close();
	}
}
